﻿Public Class frmNhanVien

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub frmNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        datNhanVien.DataSource = clsNhanVien.DsNhanVien

    End Sub
    Private Sub PhucHoiNutSua()
        If btnSua.Text = "&Lưu Lại" Then 'lai sai o day nua ne, dang tri qua
            btnSua.Text = "&Sửa"

        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoiNutSua()
        clsNhanVien.ThemNV(txtMaNV.Text, txtTenNV.Text, txtDiaChi.Text, txtDienThoai.Text, cboQuyen.Text, txtMatKhau.Text, txtChucVu.Text)
        datNhanVien.DataSource = clsNhanVien.DsNhanVien
        btnReLoad_Click(sender, e)


    End Sub

    Private Sub btnReLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        btnSua.Text = "&Sửa" 'mà nếu muốn có fim' tắt thì phải cho & vào nữa
        txtMaNV.Text = ""
        txtTenNV.Text = ""
        txtChucVu.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        txtMatKhau.Text = ""


    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        datNhanVien.DataSource = clsNhanVien.TimKiem(txtTimKiem.Text)

    End Sub

    Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMatKhau.TextChanged

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "&Sửa" Then
            txtMaNV.Text = datNhanVien.CurrentRow.Cells("MaNV").Value
            txtTenNV.Text = datNhanVien.CurrentRow.Cells("TenNV").Value
            txtDiaChi.Text = datNhanVien.CurrentRow.Cells("DiaChi").Value
            txtDienThoai.Text = datNhanVien.CurrentRow.Cells("DienThoai").Value
            cboQuyen.Text = datNhanVien.CurrentRow.Cells("Quyen").Value
            txtChucVu.Text = datNhanVien.CurrentRow.Cells("ChucVu").Value
            txtMatKhau.Text = datNhanVien.CurrentRow.Cells("MatKhau").Value
            btnSua.Text = "&Lưu lại"
        Else
            btnSua.Text = "&Sửa"
            If clsNhanVien.Sua(txtMaNV.Text, txtTenNV.Text, txtDiaChi.Text, txtDienThoai.Text, cboQuyen.Text, txtMatKhau.Text, txtChucVu.Text) = False Then
                MsgBox("Loi trong khi luu du lieu", MsgBoxStyle.Critical, "Loi")
            End If
            datNhanVien.DataSource = clsNhanVien.DsNhanVien
            btnReLoad_Click(sender, e)
        End If
    End Sub

    Private Sub datNhanVien_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datNhanVien.CellDoubleClick
        PhucHoiNutSua()
        btnSua_Click(sender, e)

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoiNutSua()
        If MsgBox("Ban có muốn xóa nhân viên có mã là " & txtMaNV.Text & " không ?", MsgBoxStyle.YesNo, "Thông báo") = MsgBoxResult.Yes Then

            If Not clsNhanVien.Xoa(txtMaNV.Text.Trim) Then
                MsgBox("Không thể xóa nhân viên có mã " & txtMaNV.Text & ".Có thể dữ liệu của nhân viên này vẫn còn tồn tại!", MsgBoxStyle.Critical, "Loi")
            End If
            datNhanVien.DataSource = clsNhanVien.DsNhanVien

        End If


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        datNhanVien.DataSource = clsNhanVien.DsNhanVien
        txtTimKiem.Text = ""

    End Sub

    Private Sub cboPhanLoaiQuyen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPhanLoaiQuyen.SelectedIndexChanged
        If cboPhanLoaiQuyen.Text = "Tất cả" Then
            datNhanVien.DataSource = clsNhanVien.DsNhanVien

        Else
            datNhanVien.DataSource = clsNhanVien.PhanLoai(cboPhanLoaiQuyen.Text)
        End If

    End Sub
End Class